<?php
	include_once '../../config.inc.php';
	
	$eve = isset($_POST['eve']) && !empty($_POST['eve']) ? $_POST['eve'] : 0;
	
	//print_r($_POST);
	//exit;
	
	if($adminId <= 0 or $eve <= 0){
		die('9');
	}
	//1为增加商品分类
	if($eve == 1){
		/*,cid,pid,cname*/
		
		try{
			$pdo->beginTransaction();//开启事务处理 
			$stmt = $pdo->prepare('select id,cid,state from kp_goods_class where cid=?');
			$stmt->execute(array($_POST['cid']));
			if($arr = $stmt->fetch(PDO::FETCH_ASSOC)){
				if($arr['state'] == 1){
					//echo '2';//2为分类已存在
					die('2');
				}else{
					$flag = false;
					$stmt = $pdo->prepare('update kp_goods_class set pid=?,cname=?,state=1 where cid=?');
					$flag = $stmt->execute(array($_POST['pid'],$_POST['cname'],$_POST['cid']));
					if($flag){
						$pdo->commit();//成功就提交
						die('1');
					}else{
						die('0');
					}
				}
				
			}
			if(!empty($_POST['pid'])){
				$stmt = $pdo->prepare('select cid from kp_goods_class where pid=?');
				$stmt->execute(array($_POST['pid']));
				$arr = $stmt->fetch(PDO::FETCH_ASSOC);
				if(count($arr) < 1){
					//echo '3';//3为上级分类不存在
					die('3');
				}
			}
			$flag = false;
			$stmt = $pdo-> prepare('insert into kp_goods_class(cid,pid,cname,state) values(?,?,?,1)');
			$flag = $stmt->execute([$_POST['cid'],$_POST['pid'],$_POST['cname']]);
			$pdo->commit();//成功就提交
			if($flag){
				echo '1';
			}else{
				echo '0';
			}
		}catch(PDOException $e){
			$pdo->rollback();
		}
	}
	//2修改商品分类:下次用存储过程
	if($eve == 2){
		/*,cid,pid,cname*/
		$flag = false;
		//1.要改分类,先要改它的子类所属
		try{
			$pdo->beginTransaction();//开启事务处理 
			//mysql超级变态语句,在update where 中不能使用 select
			//$stmt = $pdo->prepare('update kp_goods_class set cid=concat(\''.$_POST['cid'].'\',substring(cid,3)),pid=? where pid=?'); 
			$stmt = $pdo->prepare('update kp_goods_class a INNER JOIN kp_goods_class b ON a.id=? and a.cid = b.pid set b.cid=concat(\''.$_POST['cid'].'\',substring(b.cid,3)),b.pid=?'); 
			$flag = $stmt->execute(array($_POST['id'],$_POST['cid']));
			
			//2.修改分类
			$stmt = $pdo->prepare('update kp_goods_class set cid=?,pid=?,cname=? where id=?');
			$flag = $stmt->execute(array($_POST['cid'],$_POST['pid'],$_POST['cname'],$_POST['id']));
			$pdo->commit();	//提交事务
			if($flag){
				echo '1';
			}else{
				echo '0';
			}
		}catch(PDOException $e){
			$pdo->rollback();//回滚事务
			echo '0';
		}
	}
	//3.删除商品分类
	if($eve == 3){
		/*id,cid,pid,cname*/
		$flag = false;
		//如果分类下有商品则不让删除
		$stmt=$pdo->prepare('select count(gid) rows from kp_goods where gclass like ?');
		$stmt->execute(array($_POST['cid'].'%'));
		$garr = $stmt->fetch(PDO::FETCH_ASSOC);
		if($garr['rows'] > 0){
			die('分类或子分类下有商品,不能删除!');
		}
		
		$stmt = $pdo->prepare('update kp_goods_class set state = 0 where id=?');
		$flag = $stmt->execute(array($_POST['id'])); 
		if($flag){
			echo '1';
		}else{
			echo '0';
		}
	}
	//接收新增商品资料
	if($eve == 4){
		//print_r($_POST);
		//增加商品表单
		//isbn= &gname= &gphpto= &gclass=0102 &inprice= &price= &zkprice= &num= &bz=

		$stmt = $pdo->prepare('select isbn from kp_goods where isbn=?');
		$stmt->execute([$_POST['isbn']]);
		//$arrGood = $stmt->fetch(PDO::FETCH_ASSOC);
		//判断ISBN是否存在
		if($stmt->rowCount() > 0){
			die('商品货号已存在!'); 
		}
		//$sqlStr = "insert into kp_goods(isbn,gname,gphoto,gclass,inprice,price,zkprice,num,bz) select MAX(isbn)+1,'测试商品04',null,'0202',15.00,20.00,16.00,100,NULL FROM kp_goods;";
		
		//如果传过来ISNB是空的则自动来一个
		$flag = false;
		if(!empty($_POST['isbn'])){
			$stmt = $pdo->prepare('insert into kp_goods(isbn,gname,gphoto,gclass,inprice,price,zkprice,num,bz) values(?,?,?,?,?,?,?,?,?)');
			$flag = $stmt->execute([$_POST['isbn'],$_POST['gname'],$_POST['gphoto'],$_POST['gclass'],$_POST['inprice'],$_POST['price'],$_POST['zkprice'],$_POST['num'],$_POST['bz']]);
		}else{
			$stmt = $pdo->prepare("insert into kp_goods(isbn,gname,gphoto,gclass,inprice,price,zkprice,num,bz) select MAX(isbn)+1,?,?,?,?,?,?,?,? FROM kp_goods");
			$flag = $stmt->execute([$_POST['gname'],$_POST['gphoto'],$_POST['gclass'],$_POST['inprice'],$_POST['price'],$_POST['zkprice'],$_POST['num'],$_POST['bz']]);
		}
		if($flag){
			echo '1';
		}else{
			echo '增加商品失败!';
		}
		
	}
	//5为修改单条商品资料
	if($eve == 5){
		$flag = FALSE;
		$stmt = $pdo->prepare('update kp_goods set isbn=?,gname=?,gphoto=?,gclass=?,inprice=?,price=?,zkprice=?,bz=? where gid=?');
		$flag = $stmt->execute([$_POST['isbn'],$_POST['gname'],$_POST['gphoto'],$_POST['gclass'],$_POST['inprice'],$_POST['price'],$_POST['zkprice'],$_POST['bz'],$_POST['gid']]);
		if($flag){
			echo '1';
		}else{
			echo '修改商品失败!';
		}
		
	}
	//6为删除单条商品资料
	if($eve == 6){
		$flag = FALSE;
		$stmt = $pdo->prepare('update kp_goods set gzt=3 where gid=?');
		$flag = $stmt->execute([$_POST['gid']]);
		if($flag){
			echo '1';
		}else{
			echo '修改商品失败!';
		}
		
	}
	//7为修改用户资料
	if($eve == 7){
		$flag = FALSE;
		if(empty($_POST['upwd'])){
			$stmt = $pdo->prepare('update kp_usr set uname=?,email=?,sex=?,photo=?,power=?,bz=? where uid=?');
			$flag = $stmt->execute([$_POST['uname'],$_POST['email'],$_POST['sex'],$_POST['photo'],$_POST['power'],$_POST['bz'],$_POST['uid']]);
		}else{
			$stmt = $pdo->prepare('update kp_usr set uname=?,upwd=?,email=?,sex=?,photo=?,power=?,bz=? where uid=?');
			$flag = $stmt->execute([$_POST['uname'],md5($_POST['upwd']),$_POST['email'],$_POST['sex'],$_POST['photo'],$_POST['power'],$_POST['bz'],$_POST['uid']]);
		}
		if($flag){
			echo '1';
		}else{
			echo '修改用户失败!';
		}
		
	}
	//8为删除前台用户资料
	if($eve == 8){
		$flag = FALSE;
		$stmt = $pdo->prepare('update kp_usr set power=-1 where uid=?');
		$flag = $stmt->execute([$_POST['uid']]);
		if($flag){
			echo '1';
		}else{
			echo '删除用户失败!';
		}
		
	}
	//9为增加后台用户资料
	if($eve == 9){
		$flag = FALSE;
		$stmt = $pdo->prepare('insert into kp_admin(uname,upwd,uzt) values(?,?,1)');
		$flag = $stmt->execute([$_POST['uname'],md5($_POST['upwd'])]);
		if($flag){
			echo '1';
		}else{
			echo '增加管理员失败!';
		}
	}
	//10为修改后台用户资料
	if($eve == 10){
		$flag = FALSE;
		$stmt = $pdo->prepare('update kp_admin set uname=?,upwd=? where uid=?');
		$flag = $stmt->execute([$_POST['uname'],md5($_POST['upwd']),$_POST['uid']]);
		if($flag){
			echo '1';
		}else{
			echo '修改管理员失败!';
		}
	}
	//11为删除后台用户资料
	if($eve == 11){
		$flag = FALSE;
		$stmt = $pdo->prepare('delete from  kp_admin where uid=?');
		$flag = $stmt->execute([$_POST['uid']]);
		if($flag){
			echo '1';
		}else{
			echo '修改管理员失败!';
		}
	}
	//12为修改后台资讯
	if($eve == 12){
		$flag = FALSE;
		$stmt = $pdo->prepare('update kp_news set title=?,content=? where id=?');
		$flag = $stmt->execute([$_POST['title'],$_POST['content'],$_POST['id']]);
		if($flag){
			echo '1';
		}else{
			echo '修改资讯失败!';
		}
	}
	//12为删除后台资讯
	if($eve == 13){
		$flag = FALSE;
		$stmt = $pdo->prepare('delete from kp_news where id=?');
		$flag = $stmt->execute([$_POST['id']]);
		if($flag){
			echo '1';
		}else{
			echo '修改资讯失败!';
		}
	}
	//14为增加后台资讯
	if($eve == 14){
		$flag = FALSE;
		$stmt = $pdo->prepare('insert into kp_news(uid,title,content) values(?,?,?)');
		$flag = $stmt->execute([$adminId,$_POST['title'],$_POST['content']]);
		if($flag){
			echo '1';
		}else{
			echo '修改资讯失败!';
		}
	}
	//15为发货
	if($eve == 15){
		$flag = FALSE;
		$stmt = $pdo->prepare('update kp_sell_order set zt=2,expid=? where sid=?');
		$flag = $stmt->execute([$_POST['expid'],$_POST['sid']]);
		if($flag){
			echo '1';
		}else{
			echo '发货操作失败!';
		}
	}
	//16为删除订单
	if($eve == 16){
		$flag = FALSE;
		$stmt = $pdo->prepare('update kp_sell_order set zt=0,expid=? where sid=?');
		$flag = $stmt->execute([$_POST['expid'],$_POST['sid']]);
		if($flag){
			echo '1';
		}else{
			echo '发货操作失败!';
		}
	}
	//17为用户充值
	if($eve == 17){
		//$flag = FALSE;
		
		try{
			$pdo->beginTransaction(); //开启事务
			//写入日志
			$stmt = $pdo->prepare('insert into kp_money_log(adminid,uid,money,bz) values(?,?,?,?)');
			$stmt->execute([$adminId,$_POST['uid'],$_POST['money'],$_POST['bz']]);
			//修改余额
			$stmt = $pdo->prepare('update kp_usr set money=money+? where uid=?');
			$stmt->execute([$_POST['money'],$_POST['uid']]);
			
			$pdo->commit();
			echo '1';
		}catch(PDOException $e){
			$pdo->rollBack();
			echo '操作失败!'.$e;
		}
/*		
		
		if($flag){
			echo '1';
		}else{
			echo '发货操作失败!';
		}*/
	}
?>